package com.yls.except.controller;

import com.google.gson.Gson;
import com.yls.except.service.IndentService;
import com.yls.except.util.*;
import com.yls.except.util.exception.ExceptionHandle;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单controller
 * Created by Administrator on 2016/8/13.
 */
@Controller
@RequestMapping(value = "indentcontroller")
public class IndentController extends ExceptionHandle {
    @Resource(name = "indentService")
    private IndentService indentService;

    /**
     * 预下单
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "preindent.action")
    public void preindent(HttpServletRequest req, HttpServletResponse resp, HttpSession session) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String shop = session.getAttribute("shopinfo") + "";
        Map<String, Object> map = GeneralResult.jsonStrToMap(shop);
        String addressid = req.getParameter("addressid");
        if (StringIsempty.isallempty(uid))
            GeneralResult.general(resp, list, 10001, 0);
        else {
            JSONArray shopinfo = (JSONArray) map.get("shopinfo");
            if (shopinfo == null || shopinfo.length() < 0)
                GeneralResult.general(resp, list, 20001, 0);
            else {
                Map<String, Object> indentinfo = indentService.updatetIndent(uid, shopinfo, addressid);
                GeneralResult.generalMap(resp, indentinfo);
            }
        }
    }

    /**
     * 下单
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "indent.action")
    public void indent(HttpServletRequest req, HttpServletResponse resp, HttpSession session) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String json = session.getAttribute("shopinfo") + "";
        Map<String, Object> map = GeneralResult.jsonStrToMap(json);
        String addressid = req.getParameter("addressid");
        String indenttype = req.getParameter("indnettype");
        String type = req.getAttribute("type") + "";
        String reqip = IpAddressUitls.getIpAddr(req);
        if (StringIsempty.isallempty(uid) || StringIsempty.isallempty(type))
            GeneralResult.general(resp, list, 10001, 0);
        else if ("0".equals(indenttype) && StringIsempty.isallempty(addressid))
            GeneralResult.general(resp, list, 20001, 0);
        else {
            JSONArray shopinfo = (JSONArray) map.get("shopinfo");
            if (shopinfo == null || shopinfo.length() < 0)
                GeneralResult.general(resp, list, 20001, 0);
            else {
                Map<String, Object> indentinfo = indentService.insertIndent(uid, shopinfo, addressid, indenttype, reqip);
                GeneralResult.generalMap(resp, indentinfo);
                session.removeAttribute("shopinfo");
            }
        }
    }

    /**
     * 订单支付
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "payindent.action")
    public void payIndent(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String openid = req.getAttribute("openid") + "";
        String indentnum = req.getParameter("indentnum");
        String paytype = req.getParameter("paytype");
        String requrl = req.getParameter("requrl");
        String reqip = IpAddressUitls.getIpAddr(req);
        if (StringIsempty.isallempty(uid))
            GeneralResult.general(resp, list, 10001, 0);
        else if (StringIsempty.isallempty(indentnum) || StringIsempty.isallempty(paytype))
            GeneralResult.general(resp, list, 20001, 0);
        else {
            Map<String, Object> map = indentService.updateIndentInfo(uid, indentnum, paytype, reqip, openid, requrl);
            GeneralResult.generalMap(resp, map);
        }
    }

    /**
     * 获取订单列表信息
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "indentlist.action")
    public void indentList(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String indentstate = req.getParameter("indentstate");
        String page = req.getParameter("page");
        String indenttype = req.getParameter("indenttype");
        if (StringIsempty.isallempty(uid))
            GeneralResult.general(resp, list, 10001, 0);
        else {
            list = indentService.selectIndentListInfoByUid(uid, indentstate, page, indenttype);
            Map<String, Object> userinfo = indentService.selectUserinfoById(uid);
            String sparemoney = "0.00";
            if (!StringIsempty.isNullMap(userinfo))
                sparemoney = ValueJude.getDecima(userinfo.get("sparemoney") + "");

            if (StringIsempty.isNullList(list))
                GeneralResult.resultList(list, resp);
            else {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("sparemoney", sparemoney);
                map.put("code", 20000);
                map.put("data", list);
                map.put("message", "");
                Gson gson = new Gson();
                String result = gson.toJson(map);
                GeneralResult.writeMessage(resp, result);
            }
        }
    }

    /**
     * 获取订单详情
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "indentinfo.action")
    public void indentInfo(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String indentnum = req.getParameter("indentnum");
        if (StringIsempty.isallempty(uid))
            GeneralResult.general(resp, list, 10001, 0);
        else {
            Map<String, Object> indentinfo = indentService.selectIndentInfoById(indentnum);
            GeneralResult.generalMap(resp, indentinfo);
        }
    }

    /**
     * 取消订单信息
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "cancleindent.action")
    public void cancleIndent(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String indentnum = req.getParameter("indentnum");
        if (StringIsempty.isallempty(uid))
            GeneralResult.general(resp, list, 10001, 0);
        else {
            int tag = indentService.updateCancleIndent(uid, indentnum);
            GeneralResult.general(resp, list, tag, 1);
        }
    }

    /**
     * 发货通知
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "sendadvice.action")
    public void sendadvice(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String indentnum = req.getParameter("indentnum");
        String reqip = IpAddressUitls.getIpAddr(req);
        if (StringIsempty.isallempty(uid))
            GeneralResult.general(resp, list, 10001, 0);
        else {
            int tag = indentService.insertSendAdvice(uid, indentnum, reqip);
            GeneralResult.general(resp, list, tag, 1);
        }
    }

    /**
     * 确认收货
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "confirmindent.action")
    public void confirmIndent(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String indentnum = req.getParameter("indentnum");
        String reqip = IpAddressUitls.getIpAddr(req);
        if (StringIsempty.isallempty(uid))
            GeneralResult.general(resp, list, 10001, 0);
        else {
            int tag = indentService.updateConfirmIndent(uid, indentnum, reqip);
            GeneralResult.general(resp, list, tag, 1);
        }
    }

    /**
     * 删除订单
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "delindent.action")
    public void delIndent(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String indentnum = req.getParameter("indentnum");
        if (StringIsempty.isallempty(uid))
            GeneralResult.general(resp, list, 10001, 0);
        else {
            int tag = indentService.deleteIndent(uid, indentnum);
            GeneralResult.general(resp, list, tag, 1);
        }
    }

    /**
     * 获取仓储商品信息
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "shopindent.action")
    public void shopIndent(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String type = req.getAttribute("type") + "";
        String page = req.getParameter("page");
        if (StringIsempty.isallempty(uid) || StringIsempty.isallempty(type))
            GeneralResult.general(resp, list, 10001, 0);
        else {
            list = indentService.selectShopIndetByUid(uid, page);
            GeneralResult.resultList(list, resp);
        }
    }

    /**
     * 获取仓储详情
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "repertoryinfo.action")
    public void repertoryInfo(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String type = req.getAttribute("type") + "";
        String repertoryid = req.getParameter("repertoryid");
        if (StringIsempty.isallempty(uid) || StringIsempty.isallempty(type))
            GeneralResult.general(resp, list, 10001, 0);
        else {
            Map<String, Object> repertory = indentService.selectRepertoryInfoById(repertoryid, uid);
            GeneralResult.generalMap(resp, repertory);
        }
    }

    /**
     * 购买vip
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "buyvip.action")
    public void buyVip(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String type = req.getAttribute("type") + "";
        String openid = req.getAttribute("openid") + "";
        String paytype = req.getParameter("paytype");
        String requrl = req.getParameter("requrl");
        String reqip = IpAddressUitls.getIpAddr(req);
        if (StringIsempty.isallempty(uid))
            GeneralResult.general(resp, list, 10001, 0);
        else if ("1".equals(type) || "2".equals(type))
            GeneralResult.general(resp, list, 20063, 0);
        else {
            Map<String, Object> indentinfo = indentService.insertBuyVip(uid, paytype, reqip, openid, requrl);
            GeneralResult.generalMap(resp, indentinfo);
        }
    }

    /**
     * 仓储生成订单
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "repertoryindent.action")
    public void repertoryIndent(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String type = req.getAttribute("type") + "";
        String json = req.getParameter("json");
        String reqip = IpAddressUitls.getIpAddr(req);
        if (StringIsempty.isallempty(uid) || StringIsempty.isallempty(type))
            GeneralResult.general(resp, list, 10001, 0);
        else if (StringIsempty.isallempty(json))
            GeneralResult.general(resp, list, 20001, 0);
        else {
            Map<String, Object> map = GeneralResult.jsonStrToMap(json);
            if (StringIsempty.isNullMap(map))
                GeneralResult.general(resp, list, 20001, 0);
            else {
                JSONArray addressids = (JSONArray) map.get("addressids");
                JSONArray shopinfo = (JSONArray) map.get("shopinfo");
                if (addressids == null || addressids.length() <= 0 || shopinfo == null || shopinfo.length() <= 0)
                    GeneralResult.general(resp, list, 20001, 0);
                else {
                    Map<String, Object> indent = indentService.insertRepertoryIndent(uid, addressids, shopinfo, reqip);
                    GeneralResult.generalMap(resp, indent);
                }
            }
        }
    }

    /**
     * 仓储运费计算
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "repertorypay.action")
    public void repertoryPay(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String type = req.getAttribute("type") + "";
        String openid = req.getAttribute("openid") + "";
        String typeid = req.getParameter("typeid");
        String paytype = req.getParameter("paytype");
        String requrl = req.getParameter("requrl");
        String reqip = IpAddressUitls.getIpAddr(req);
        if (StringIsempty.isallempty(uid) || StringIsempty.isallempty(type))
            GeneralResult.general(resp, list, 10001, 0);
        else if (StringIsempty.isallempty(typeid) || StringIsempty.isallempty(paytype))
            GeneralResult.general(resp, list, 20001, 0);
        else {
            Map<String, Object> indent = indentService.updateRepertoryPay(uid, typeid, paytype, reqip, openid, requrl);
            GeneralResult.generalMap(resp, indent);
        }
    }

    /**
     * 获取仓储订单信息
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "getrepertoryindent.action")
    public void getRepertoryIndent(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String indentstate = req.getParameter("indentstate");
        String page = req.getParameter("page");
        if (StringIsempty.isallempty(uid))
            GeneralResult.general(resp, list, 10001, 0);
        else {
            list = indentService.selectRepertoryIndent(uid, indentstate, page);
            GeneralResult.resultList(list, resp);
        }
    }

    /**
     * 取消发货
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "cancleRepertory.action")
    public void cancleRepertory(HttpServletRequest req, HttpServletResponse resp) {
        String uid = req.getAttribute("uid") + "";
        String indentnum = req.getParameter("indentnum");
        if (StringIsempty.isallempty(uid))
            GeneralResult.general(resp, null, 10001, 0);
        else if (StringIsempty.isallempty(indentnum))
            GeneralResult.general(resp, null, 20001, 0);
        else {
            int tag = indentService.updateRepertoryCancle(uid, indentnum);
            GeneralResult.general(resp, null, tag, 1);
        }
    }

    /**
     * 发货订单支付
     */
    @RequestMapping(value = "repertoryPrePay.action")
    public void repertoryPrePay(HttpServletRequest req, HttpServletResponse resp) {
        String uid = req.getAttribute("uid") + "";
        String indentnum = req.getParameter("indentnum");
        String paytype = req.getParameter("paytype");
        String reqip = req.getParameter("reqip");
        String openid = req.getAttribute("openid") + "";
        if (StringIsempty.isallempty(uid))
            GeneralResult.general(resp, null, 10001, 0);
        else if (StringIsempty.isallempty(indentnum))
            GeneralResult.general(resp, null, 20001, 0);
        else {
            Map<String, Object> indentinfo = indentService.updateRepertorySinglePay(indentnum, uid, paytype, reqip, openid);
            GeneralResult.generalMap(resp, indentinfo);
        }
    }

    /**
     * 获取js签名
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "getSignature.do")
    public void getSignature(HttpServletRequest req, HttpServletResponse resp) {
        String requrl = req.getParameter("requrl");
        String timestamp = req.getParameter("timestamp");
        String nonceStr = req.getParameter("nonceStr");
        String access_token = WXInfo.getToken();
        String ticket = "";
        if (!StringIsempty.isallempty(access_token)) {
            ticket = WXInfo.getticket(access_token);
            String signature = WXInfo.sign(ticket, requrl, nonceStr, timestamp);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("signature", signature);
            Logger logger = Logger.getLogger(IndentController.class);
            logger.info("signature:" + signature);
            GeneralResult.generalMap(resp, map, 20000);
        }
    }

    /**
     * 余额充值
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "recharge.action")
    public void recharge(HttpServletRequest req, HttpServletResponse resp) {
        String uid = req.getAttribute("uid") + "";
        String paytype = req.getParameter("paytype") + "";
        String bntid = req.getParameter("bntid") + "";
        String openid = req.getAttribute("openid") + "";
        String reqip = IpAddressUitls.getIpAddr(req);
        if(StringIsempty.isallempty(uid))
            GeneralResult.general(resp, null, 10001, 0);
        else if(StringIsempty.isallempty(paytype) || StringIsempty.isallempty(bntid))
            GeneralResult.general(resp, null, 20001, 0);
        else {
            Map<String, Object> indentinfo = indentService.insertRecharge(uid, paytype, bntid, openid, reqip);
            GeneralResult.generalMap(resp, indentinfo);
        }
    }

    /**
     * 根据订单id查询物流信息
     * @param req
     * @param resp
     */
    @RequestMapping(value = "logisticinfo.action")
    public void logisticinfo(HttpServletRequest req, HttpServletResponse resp) {
        String uid = req.getAttribute("uid") + "";
        String indentnum = req.getParameter("indentnum");
        if(StringIsempty.isallempty(uid))
            GeneralResult.general(resp, null, 10001, 0);
        else if(StringIsempty.isallempty(indentnum))
            GeneralResult.general(resp, null, 20001, 0);
        else {
            Map<String, Object> logisticinfo = indentService.selectLogisticInfo(indentnum);
            GeneralResult.generalMap(resp, logisticinfo);
        }
    }
}

